package org.jfk.sys.org;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;
import org.jfk.sys.user.User;

public class OrgController extends BaseController {
	private static Logger logger = Logger.getLogger(OrgController.class);
	
	public void edit() {
		setAttr("org", Org.dao.findById(getParaToInt()));
		setBackUrl("");
	}	


	public void update(){
		Org org = getModel(Org.class);
		org.update();
		super.renderJson();
	}	
	
	public void save() throws Exception {
		
		Org org = getModel(Org.class);
		if (org.get("id") == null){
			User currUser = super.getCurrUser();
			org.set("creator", currUser.getInt("id"));			
			
			org.save();
		}else{
			org.update();
		}
		super.renderJson();
	}	
	
	public void list(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "orgName", "组织名称" },
				{ "pid", "上级组织ID" },
				{ "description", "描述" },
				{ "showOrder", "显示顺序" },
				{ "status", "数据状态" },
				{ "createTime", "创建时间" }, 
				{ "creator_loginName", "创建人" }
				};
		super.setAttrs();
		
		setAttr("page",Org.dao.paginate(this));
	}	

	public void del() {
		String id = getPara("id");
		Org.dao.deleteById(id);
		super.renderText("test del");
	}

	
	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			Org.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}
	
	public void ajaxLoadAll4Ztree(){
		List<Map> ztreeList = Org.dao.findAll4Ztree();
		super.renderJson(ztreeList);
	}
}
